package com.cardbaobao.cardbabyclient.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.widget.ImageView;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.utils.https.support.HttpsImageDownloader;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.utils.L;

/**
 * ImageLoader 图片加载工具
 */
public class ImageLoaderUtil {
    private static ImageLoader imageLoader;
    private static int defaultImageRes = R.mipmap.img_default_banner;

    private static ImageLoader getInstance() {
        if (imageLoader == null) {
            synchronized (ImageLoaderUtil.class) {
                if (imageLoader == null) {
                    Context context = ApplicationHelper.getApplicationContext();
                    ImageLoaderConfiguration configuration =
                            new ImageLoaderConfiguration.Builder(context).
                                    imageDownloader(new HttpsImageDownloader(context)).
                                    diskCacheSize(50 * 1024 * 1024).
                                    diskCacheFileCount(100).
                                    build();
                    L.writeLogs(false);
                    imageLoader = ImageLoader.getInstance();
                    imageLoader.init(configuration);
                }
            }
        }
        return imageLoader;
    }

    /**
     * 加载图片
     *
     * @param imageUrl  图片url地址
     * @param imageView 需要加载图片的控件
     */
    public static void displayImage(String imageUrl, ImageView imageView) {
        DisplayImageOptions options = new DisplayImageOptions.Builder().
                cacheInMemory(true).
                cacheOnDisk(true).
                showImageOnLoading(defaultImageRes).
                showImageForEmptyUri(defaultImageRes).
                showImageOnFail(defaultImageRes).
                bitmapConfig(Bitmap.Config.RGB_565).build();
        getInstance().displayImage(imageUrl, imageView, options);
    }

    public static void displayImage(String imageUrl, ImageView imageView, int radian) {
        DisplayImageOptions options = new DisplayImageOptions.Builder().
                cacheInMemory(true).
                cacheOnDisk(true).
                showImageOnLoading(defaultImageRes).
                showImageForEmptyUri(defaultImageRes).
                showImageOnFail(defaultImageRes).
                bitmapConfig(Bitmap.Config.RGB_565)
                .displayer(new RoundedBitmapDisplayer(radian)).build();
        getInstance().displayImage(imageUrl, imageView, options);
    }

    /**
     * 加载图片，图片加载时有占位图，图片加载失败时显示默认图片
     *
     * @param imageUrl        图片url地址
     * @param imageView       加载图片的控件
     * @param defaultImageRes 默认图片
     */
    public static void displayImageWithPlaceHolder(String imageUrl,
                                                   ImageView imageView,
                                                   int defaultImageRes) {
        DisplayImageOptions options = new DisplayImageOptions.Builder().
                showImageOnLoading(defaultImageRes).
                showImageForEmptyUri(defaultImageRes).
                showImageOnFail(defaultImageRes).
                cacheInMemory(true).
                cacheOnDisk(true)
                .bitmapConfig(Bitmap.Config.RGB_565).build();
        getInstance().displayImage(imageUrl, imageView, options);
    }


    /**
     * 加载图片，图片显示效果为圆角，图片加载时有占位图，图片加载失败时显示默认图片
     *
     * @param imageUrl        图片url地址
     * @param imageView       加载图片的控件
     * @param defaultImageRes 默认显示的图片
     * @param radian          图片显示的圆角
     */
    public static void displayImageWithPlaceHolder(String imageUrl,
                                                   ImageView imageView,
                                                   int defaultImageRes,
                                                   int radian) {
        DisplayImageOptions options = new DisplayImageOptions.Builder().
                showImageOnLoading(defaultImageRes).
                showImageForEmptyUri(defaultImageRes).
                showImageOnFail(defaultImageRes).
                cacheInMemory(true).
                cacheOnDisk(true).
                bitmapConfig(Bitmap.Config.RGB_565).
                displayer(new RoundedBitmapDisplayer(radian)).build();
        getInstance().displayImage(imageUrl, imageView, options);
    }
}
